package com.rslai.base.tool.servertest.database.postgresql;

import com.rslai.base.tool.servertest.database.QunitDataTypeFactory;

import java.util.Arrays;
import java.util.Collection;

import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.datatype.DataTypeException;
import org.dbunit.ext.postgresql.GenericEnumType;
import org.dbunit.ext.postgresql.InetType;
import org.dbunit.ext.postgresql.IntervalType;
import org.dbunit.ext.postgresql.UuidType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PostgresqlDataTypeFactory extends QunitDataTypeFactory {
    private static final Logger logger = LoggerFactory.getLogger(org.dbunit.ext.postgresql.PostgresqlDataTypeFactory.class);

    private static final Collection DATABASE_PRODUCTS = Arrays.asList(new String[]{"PostgreSQL"});

    public Collection getValidDbProducts() {
        return DATABASE_PRODUCTS;
    }

    public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException {
        logger.debug("createDataType(sqlType={}, sqlTypeName={})", String.valueOf(sqlType), sqlTypeName);

        if (sqlType == 1111) {
            if ("uuid".equals(sqlTypeName))
                return new UuidType();
            if ("interval".equals(sqlTypeName))
                return new IntervalType();
            if ("inet".equals(sqlTypeName))
                return new InetType();
            if ("hstore".equals(sqlTypeName))
                return new HStoreDataType();
            if ("ltree".equals(sqlTypeName))
                return new LTreeDataType();
            if ("ARRAY".equals(sqlTypeName)) {
                return new ArrayDataType();
            }
            if (isEnumType(sqlType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Custom enum type used for sqlTypeName {} (sqlType '{}')", new Object[]{sqlTypeName, Integer.valueOf(sqlType)});
                }
                return new GenericEnumType(sqlTypeName);
            }
        }

        return super.createDataType(sqlType, sqlTypeName);
    }

    public boolean isEnumType(int sqlType) {
        return 1111 == sqlType;
    }
}